package com.guisu.material.recyclerview

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import android.widget.ProgressBar
import android.widget.TextView
import com.guisu.material.R

class CommonLoadCreator : LoadViewCreator() {
    private var tv: TextView? = null
    private var pb: ProgressBar? = null
    private var loadView: View? = null
    override fun getLoadView(context: Context, parent: ViewGroup): View {
        val loadView: View =
            LayoutInflater.from(context).inflate(R.layout.layout_load_footer_view, parent, false)
        tv = loadView.findViewById(R.id.tv)
        pb = loadView.findViewById(R.id.pb)
        this.loadView = loadView
        return loadView
    }

    override fun onPull(currentDragHeight: Int, loadViewHeight: Int, currentLoadStatus: Int) {
        if (currentLoadStatus == LoadRefreshRecyclerView.LOAD_STATUS_PULL_DOWN_REFRESH) {
            tv?.text = "上拉加载更多"
        }
        if (currentLoadStatus == LoadRefreshRecyclerView.LOAD_STATUS_LOOSEN_LOADING) {
            tv?.text = "松开加载更多"
        }
    }

    override fun onLoading() {
        tv?.visibility = View.GONE
        pb?.visibility = View.VISIBLE
        val animation = RotateAnimation(
            0f,
            720f,
            Animation.RELATIVE_TO_SELF,
            0.5f,
            Animation.RELATIVE_TO_SELF,
            0.5f
        )
        animation.duration = 2000
        animation.repeatCount = -1
        pb?.startAnimation(animation)
    }

    override fun onStopLoad() {
        //停止加载的时候
        pb?.rotation = 0f
        pb?.clearAnimation()
        pb?.visibility = View.GONE
        tv?.text = "上拉加载更多"
        tv?.visibility = View.VISIBLE
    }
}

